package sf.view.fields;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JLabel;
import javax.swing.JTextField;

import sf.model.fields.AbstractFieldModel;
import sf.model.fields.StringFieldModel;
import sf.model.fields.event.FieldChangeEvent;

public class StringField extends AbstractField
{

	private static final long serialVersionUID = -5489264657652534901L;

	private class StringFieldController implements ActionListener,
			FocusListener
	{

		
		public void actionPerformed(ActionEvent e)
		{
			// TODO Auto-generated method stub

		}

		
		public void focusGained(FocusEvent e)
		{
			// TODO Auto-generated method stub

		}

		
		public void focusLost(FocusEvent e)
		{
			// TODO Auto-generated method stub

		}

	}

	private StringFieldModel model;
	private StringFieldController controller;

	private JTextField inputField;

	public StringField()
	{
		JLabel label = new JLabel(getLabelText());
		add(label, BorderLayout.WEST);

		controller = new StringFieldController();

		inputField = new JTextField();
		inputField.addActionListener(controller);
		inputField.addFocusListener(controller);
		add(inputField, BorderLayout.CENTER);

	}

	
	public AbstractFieldModel getModel()
	{
		return model;
	}

	
	public Object getValue()
	{
		return model.getValue();
	}

	
	public void setModel(AbstractFieldModel model)
	{
		this.model = (StringFieldModel) model;
		this.model.addChangeListener(this);
		inputField.setText((String) this.model.getValue());
	}

	
	public void setValue(Object value)
	{
		model.setValue(value);
	}

	
	public void changePerformed(FieldChangeEvent event)
	{
		inputField.setText(((String) model.getValue()).trim());
	}

	
	public void setEditable(boolean editable)
	{
		// TODO Auto-generated method stub

	}

}
